<?php


namespace app\api\service\hotel;


use app\api\model\HotelRoom;
use app\api\model\HotelRoomOrder;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class JudgeRoom extends Token
{
    /**
     * 1.获取用户信息  判断用户信息
     * 2.查询判断当前房间是否有数据
     * 3.判断是几天预订时间  最多可以预订两天 查询当前房间在预订的时间有没有预订
     * 4.查询当前用户有没有还没有退的当前房间
     */

    // 用户信息
    private $user;

    // 预订开始时间
    private $startDate;

    // 预订结束时间
    private $endDate;

    // 房间信息
    private $hotelRoom;

    function __construct($hotelRoomId,$startDate,$endDate)
    {
        $this->hotelRoom = HotelRoom::get($hotelRoomId);
        $this->user = User::get(self::getCurrentUid());
        $this->startDate = $startDate;
        $this->endDate = $endDate;
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeUser();
    }

    // 判断用户信息
    private function judgeUser()
    {
        self::judgeMember();
        return $this->judgeData();
    }

    // 判断房间信息
    private function judgeData()
    {
        if(!$this->hotelRoom) {
            throw new ParameterException([
                'msg' => [
                    'room' => '请重新选择酒店房间'
                ]
            ]);
        }
       return $this->judgeOrder();
    }

    // 判断当前房间当前时间有没有人预订
    private function judgeOrder()
    {
        // 判断预订几天
        $datetime_start = strtotime($this->startDate);
        $datetime_end = strtotime($this->endDate);
        if($this->startDate < date('Y-m-d')) {
            throw new ParameterException([
                'msg' => [
                    'room' => '预订时间已过'
                ]
            ]);
        }
        $days = round(($datetime_end - $datetime_start) / 3600 / 24);
        if($days > 2) {
            throw new ParameterException([
                'msg' => [
                    'room' => '该房间最多预订2天'
                ]
            ]);
        }
        $where = [
            [ 'hotel_room_id','=', $this->hotelRoom['id'] ],
            [ 'start_date','<=', $this->startDate ],
            [ 'end_date','>=', $this->startDate ],
            [ 'status', 'in', '2,4,5'],
        ];
        $where1 = [
            [ 'hotel_room_id', '=', $this->hotelRoom['id'] ],
            [ 'start_date', '<=', $this->endDate ],
            [ 'end_date', '>=', $this->endDate ],
            [ 'status', 'in', '2,4,5' ],
        ];
        $hotelRoomOrder = HotelRoomOrder::order('id asc')->whereOr([$where, $where1])->find();
        if($hotelRoomOrder) {
            throw new ParameterException([
                'msg' => [
                    'room' => '该酒店房间在该时间已被预订'
                ]
            ]);
        }
        return $this->judgeRoomOrder();
    }
    
    // 查询当前用户有没有还没有退的当前房间
    private function judgeRoomOrder()
    {
       $roomOrder = HotelRoomOrder::where('user_id',$this->user['id'])
           ->where('status',2)->find();
       if($roomOrder) {
           throw new ParameterException([
               'msg' => [
                   'room' => '已经有订单'
               ]
           ]);
       }
       return [ 'isValid' =>true ];
    }
}